package com.bsoft.gol.hcs.his.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.bsoft.gol.hcs.common.service.BaseDataService;
import hcn.util.BaseResponse;
import com.bsoft.gol.hcs.his.bean.vo.DicFileVo;
import com.bsoft.gol.hcs.his.service.IDictionaryHisService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import com.bsoft.gol.hcs.satisfaction.bean.qo.DictionaryQo;
import com.bsoft.gol.hcs.satisfaction.bean.vo.DictionaryVo;
import com.bsoft.gol.hcs.specialist.service.impl.DictionaryServiceImpl;
import com.bsoft.gol.hcs.utils.JSONDateUtil;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("dictionaryHisService")
public class DictionaryHisServiceImpl implements IDictionaryHisService {

    private final static Logger logger = LoggerFactory.getLogger(DictionaryServiceImpl.class);
    @Autowired
    private BaseDataService baseDataService;

    @Override
    public BaseResponse<List<DictionaryVo>> getHisDictionaryList(DictionaryQo qo){
        BaseResponse<List<DictionaryVo>>  baseResponse = new BaseResponse<>();
        List<DictionaryVo> dictionaryVos = new ArrayList<>();
        String data = "";
        String code = "200";
        String msg = "";
        String url = "";
        String hisUrl = "";
        String examUrl = "";
        String checkUrl ="";
        String type = qo.getDicType();
//        ParamsOrgQo request = new ParamsOrgQo();
//        request.setType(type);
//        request.setOrganizationId("420112007");
//        //查询所有机构
//        BaseResponse listBaseResponse = baseDataService.queryOrganizationList(request);
//        if(CommonUtils.isNotEmpty(listBaseResponse) && listBaseResponse.getCode()==200 && null!= listBaseResponse.getData()){
//            List<Map> dataList = (List<Map>) listBaseResponse.getData();
//            if(CommonUtils.isNotEmpty(dataList)){
//                for (Map datum : dataList) {
//                    //HIS服务地址
//                     hisUrl = (String) datum.get("hisUrl");
//                    //检验服务地址
//                     examUrl = (String) datum.get("examUrl");
//                    //检查服务地址
//                     checkUrl = (String) datum.get("checkUrl");
//                }
//            }
//        }


//        try{
//        switch (type){
//            case "3":
//
////                url = checkUrl+ Constants.DICTIONATY_RECORD;break;
//            case "4":
////                url =examUrl+ Constants.DICTIONATY_RECORD;break;
//            case "1":
////                url=hisUrl+ Constants.DICTIONATY_RECORD;break;
//            case "2":
//
//            case "5":
//
//            case "6":
//        }
//        String json = JSON.toJSONString(qo, SerializerFeature.WriteDateUseDateFormat);
//        String result =  HttpClient.doPost(url, json);
//        logger.info("查询字典记录："+result);
//        code = JSON.parseObject(result).getString("code");
//        data = JSON.parseObject(result).getString("data");
//        msg = JSON.parseObject(result).getString("message");
//        if(CommonUtils.isNotEmpty(data) || "200".equals(code)){
//            dictionaryVos = JSONObject.parseArray(data,DictionaryVo.class);
//            baseResponse.setCode(Integer.parseInt(code));
//            baseResponse.setMessage(msg);
//            baseResponse.setData(dictionaryVos);
//        }else{
//            baseResponse.setCode(Integer.parseInt(code));
//            baseResponse.setMessage(msg);
//        }
//        }catch (Exception e){
//            baseResponse.setCode(Integer.parseInt(code));
//            baseResponse.setMessage(msg);
//            logger.error(msg);
//        }

        String jsonContext = JSONDateUtil.readFile("dictionary");
        String list = JSON.parseObject(jsonContext).getString("list");
        List<DicFileVo> inDeptForDoctorResponses =  JSONArray.parseArray(list, DicFileVo.class);
        for (DicFileVo dicFileVo:inDeptForDoctorResponses) {
            if(dicFileVo.getType().equals(type)){
                dictionaryVos.addAll(dicFileVo.getDictionaryVoList());
                break;
            }
        }
        baseResponse.setCode(Integer.parseInt(code));
        baseResponse.setMessage(msg);
        baseResponse.setData(dictionaryVos);
        return baseResponse;
    }
}
